package org.ucas.faker.web.controller;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.ucas.faker.web.dao.DepotDao;
import org.ucas.faker.web.dao.SpecialKeywordDao;
import org.ucas.faker.web.dao.UserSpTreeDao;
import org.ucas.faker.web.entity.Depot;
import org.ucas.faker.web.entity.SpecialKeyword;
import org.ucas.faker.web.entity.User;
import org.ucas.faker.web.entity.UserSpTree;
import org.ucas.faker.web.model.KeywordNode;

@RestController
public class SpecialKeywordController extends BaseController{
	
	@Autowired
	SpecialKeywordDao skDao;
	
	@Autowired
	DepotDao dpDao;
	
	@Autowired
	UserSpTreeDao sptreeDao;
	
	@RequestMapping(value="/sptree/all", method = RequestMethod.GET)
	public List<UserSpTree> SPTreeAll(){
		User user = currentUser();
	    if(user == null) {
	      user = userService.findByAccount("root");
	      context().userId = user.getId();
	    }

		return sptreeDao.findByUserId(user.getId());
	}

	@RequestMapping(value="/sptree/newsptree", method = RequestMethod.GET)
	public String newSpTree(@RequestParam("treeName") String treeName){
		User user = currentUser();
	    if(user == null) {
	      user = userService.findByAccount("root");
	      context().userId = user.getId();
	    }
	    
	    UserSpTree ust = new UserSpTree();
	    ust.setName(treeName);
	    ust.setUserId(user.getId());
	    ust = sptreeDao.save(ust);	    
	    
	    
		SpecialKeyword spkey = new SpecialKeyword();
		spkey.setTreeId(ust.getId().intValue());
		spkey.setParentId(0);
		spkey.setKeywordValue("技术特征");
		skDao.save(spkey);
		
		SpecialKeyword spkey2 = new SpecialKeyword();
		spkey2.setTreeId(ust.getId().intValue());
		spkey2.setParentId(0);
		spkey2.setKeywordValue("技术功效");
		spkey2 = skDao.save(spkey2);

//		for(SpecialKeyword sk:skDao.findByFlowId(1)){
//			System.out.print(sk.getKeywordValue());
//		}
		return ust.getId().toString();
	}
	
	@RequestMapping(value="/sptree/renamesptree", method = RequestMethod.GET)
	public String renameSpTree(@RequestParam("treeId") Long treeId,
			@RequestParam("treeName") String treeName){
		UserSpTree ust = sptreeDao.findOne(treeId);

		ust.setName(treeName);
		sptreeDao.save(ust);
		return "ok";
	}
	
	@RequestMapping(value="/sptree/removesptree", method = RequestMethod.GET)
	public String removeSpTree(@RequestParam("treeId") Long treeId,
			@RequestParam("treeName") String treeName){
		skDao.deleteByTreeId(treeId);
		sptreeDao.delete(treeId);

		return "ok";
	}
	
	
	@RequestMapping(value="/sptree/readflow", method = RequestMethod.GET)
	public List<SpecialKeyword> read(@RequestParam("treeId") Long treeId){
//		for(SpecialKeyword sk:skDao.findByFlowId(1)){
//			System.out.print(sk.getKeywordValue());
//		}
		 User user = currentUser();
		    if(user == null) {
		      user = userService.findByAccount("root");
		      context().userId = user.getId();
		    }
		    
		//return skDao.findByFlowId(Integer.parseInt(user.getId().toString()));
		    return skDao.findByTreeId(treeId);
	}
	
	@RequestMapping(value="/fk/savekw", method = RequestMethod.GET)
	public SpecialKeyword savekeyword(@RequestParam("flowid") Integer flowId,@RequestParam("keywordValue") String keyvalue,
			@RequestParam("parentid") Integer parentId){
		SpecialKeyword sp = new SpecialKeyword();
		sp.setTreeId(flowId);
		sp.setKeywordValue(keyvalue);
		sp.setParentId(parentId);
		return skDao.save(sp);
	}
	
	@RequestMapping(value="/fk/savesibkw", method = RequestMethod.GET)
	public SpecialKeyword saveSiblingKeyword(@RequestParam("flowid") Integer flowId,@RequestParam("keywordValue") String keyvalue,
			@RequestParam("parentid") Integer parentId){
		SpecialKeyword sp = new SpecialKeyword();
		sp.setTreeId(flowId);
		sp.setKeywordValue(keyvalue);
		sp.setParentId(parentId);
		return skDao.save(sp);
	}
	
	
	@RequestMapping(value="/fk/removekw", method = RequestMethod.POST)
	public String deletekeyword(@RequestParam("listParam[]") List<Long> parentId){
		for(Long i:parentId){
			//skDao.deleteById(i);
			skDao.delete(i);
			//System.out.println(i);
		}
		return "ok";
	}
	
	
	@RequestMapping(value="/fk/updatekw", method = RequestMethod.GET)
	public String updatekeyword(@RequestParam("id") Long id, @RequestParam("keywordValue") String keywordValue){
		//System.out.println(id);
		//System.out.println(keywordValue);
		skDao.setKeywordValueById(keywordValue, id);
	
		return "ok";
	}

}
